package com.wbt.bambootown.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wbt.bambootown.R;
import com.wbt.bambootown.entity.CameraPic;
import com.wbt.bambootown.mvp.contract.CameraPicContract;
import com.wbt.bambootown.mvp.frame.MvpActivity;
import com.wbt.bambootown.mvp.frame.base.BaseView;
import com.wbt.bambootown.mvp.presenter.CameraPicPresenter;
import com.wbt.bambootown.ui.adapter.CameraPicAdapter;
import com.wbt.bambootown.utils.ActivityManagerUtil;
import com.wbt.bambootown.utils.EventCenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CameraPicActivity extends MvpActivity<CameraPicPresenter> implements CameraPicContract.View {

    @BindView(R.id.rv_pic)
    XRecyclerView mRvPic;

    private int devicesId;

    private int pageIndex = 1;
    private int pageSize = 10;
    private CameraPicAdapter mAdapter;

    private boolean isFresh;

    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        Toast.makeText(this , errorMsg , Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showPicSuccess(List<CameraPic> cameraPics) {
        dismissProgressDialog();
        if(isFresh){
            mAdapter.setList(cameraPics);
            isFresh = false;
            mRvPic.refreshComplete();
        }else{
            mAdapter.appendList(cameraPics);
            mRvPic.loadMoreComplete();
        }
    }

    @OnClick(R.id.iv_back)
    public void onClick(){
        ActivityManagerUtil.create().finishActivity();
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {
        immersionBar
                .fitsSystemWindows(true)
                .statusBarColor(R.color.theme_color)
                .init();
    }

    @Override
    public void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mAdapter = new CameraPicAdapter(this);
        mRvPic.setLayoutManager(layoutManager);
        mRvPic.setAdapter(mAdapter);

        devicesId = getIntent().getIntExtra("devices_id" , 0);
        mPresenter.getCameraPic(devicesId , "" , "" , pageIndex , pageSize);
        showProgressDialog();

        mRvPic.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isFresh = true;
                pageIndex = 1;
                mPresenter.getCameraPic(devicesId , "" , "" , pageIndex , pageSize);
            }

            @Override
            public void onLoadMore() {
                pageIndex++;
                mPresenter.getCameraPic(devicesId , "" , "" , pageIndex , pageSize);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public Object getContentView() {
        return R.layout.activity_camera_pic;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mRvPic != null){
            mRvPic.destroy();
            mRvPic = null;
        }
    }
}
